package pwd.allen;

import java.awt.*;

public class Explode {

	int x,y;
	private boolean live=true;
	private TankClient tc = null;
	private static Image[] explodeImages = null;
	private static Integer[] offsets = null;
	private SpeedUtil speedUtil = new SpeedUtil(() -> {run();}, 50);

	static {
		Toolkit tk=Toolkit.getDefaultToolkit();
		explodeImages = new Image[] {
				tk.getImage(Tank.class.getClassLoader().getResource("images/1.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/2.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/3.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/4.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/5.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/6.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/7.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/8.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/9.gif")),
				tk.getImage(Tank.class.getClassLoader().getResource("images/10.gif"))
		};
		offsets = new Integer[] {
				5,
				9,
				12,
				15,
				18,
				20,
				23,
				25,
				27,
				30
		};
	}
	int step = 0;

	public Explode(int x, int y, TankClient tc) {
		this.x = x;
		this.y = y;
		this.tc = tc;
	}
	public void draw(Graphics g){
		if(!live){
			tc.explodes.remove(this);
			return;
		}
		if(step == explodeImages.length){
			live = false;
			step = 0;
			return;
		}
		Color c = g.getColor();
		g.setColor(Color.orange);
		g.drawImage(explodeImages[step],x-offsets[step],y-offsets[step],null);
		g.setColor(c);
		speedUtil.run();
//		run();
	}

	private void run() {
		step++;
	}
}
